package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 分类业务层
 */

@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 分类分页查询
     * @param categoryPageQueryDTO 分页查询参数
     * @return PageResult
     */
    @Override
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());

        Page<Category> pages = categoryMapper.pageQuery(categoryPageQueryDTO);

        return new PageResult(pages.getTotal(),pages.getResult());
    }

    /**
     * 修改分类信息
     * @param categoryDTO 修改信息
     */
    @Override
    public void update(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);

        categoryMapper.update(category);
    }

    /**
     * 启用或禁用分类状态
     * @param status 状态
     * @param id id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        Category category = Category.builder()
                .id(id)
                .status(status).build();

        categoryMapper.update(category);
    }

    /**
     * 新增分类信息
     * @param categoryDTO 新增信息
     */
    @Override
    public void save(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);

        //设置属性

        category.setStatus(StatusConstant.DISABLE);

        categoryMapper.save(category);

    }

    /**
     * 根据id删除分类
     * @param id 要删除的id
     */
    @Override
    public void deleteById(Long id) {
        //判断该分类下是否有关联菜品
        Integer count = dishMapper.countByCategoryId(id);
        if(count > 0){
            //当前分类有菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }

        //判断该分类是否有套餐
        count = setmealMapper.countByCategoryId(id);
        if(count>0){
            //当前分类有关联的套餐
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }

        //根据id删除分类
        categoryMapper.deleteById(id);
    }

    /**
     * 根据类型查询分类
     * @param type 分类类型
     * @return list
     */
    @Override
    public List<Category> list(Integer type) {
        return categoryMapper.list(type);
    }
}
